package com.hongxun.budget.ui.fragment.sg;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hongxun.budget.R;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.budget.utils.SgStrKey;
import com.hongxun.budget.utils.StringUtils;
import com.hongxun.entity.ImageUrl;
import com.hongxun.greenentity.tk.DPictureEntity;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;
import com.previewlibrary.GPreviewBuilder;
import com.yuyh.library.imgsel.widget.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 现场照片（材料现场堆放照片/项目施工进度现场照片）
 */
public class PhotoScene extends BaseListFragment implements Handler.Callback {
    private String uuid;
    private SuperAdapter superAdapter;
    private List<DPictureEntity> photosList = new ArrayList<>();
    private Handler handler = new Handler(this);

    public PhotoScene() {
    }

    public List<DPictureEntity> getDatas() {
        return photosList;
    }

    @Override
    public void initView() {
        super.initView();
        list.addItemDecoration(new DividerGridItemDecoration(context));
        new Thread(new Runnable() {
            @Override
            public void run() {
                String pGuid = String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, ""));
                photosList.addAll(SgDBUtils.getDPictureList(pGuid, uuid, "材料,xc"));
                handler.sendEmptyMessage(1);
            }
        }).start();
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getGridLayoutManager(context, 3);
    }

    @Override
    public SuperAdapter getAdapter() {
        uuid = getArguments().getString("uuid");
        superAdapter = new SuperAdapter<DPictureEntity>(R.layout.sg_content_photo_label1) {
            @Override
            public void bindView(ViewHolder viewHolder, final DPictureEntity itemBean, final int i) {
                ImageView imageView = viewHolder.getView(R.id.imageView);
                ImageButton imageButton = viewHolder.getView(R.id.imageButton);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                int i1 = getActivity().getResources().getDisplayMetrics().widthPixels / 3;
                Glide.with(context).load(SgDBUtils.getUrl(itemBean)).override(i1, 120).into(imageView);
                imageButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        photosList.remove(itemBean);
                        SgDBUtils.delDPictureEntity(itemBean);
                        handler.sendEmptyMessage(1);
                    }
                });
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        List<ImageUrl> urlList = new ArrayList<>();
                        for (DPictureEntity data : photosList) {
                            String url = String.valueOf(SgDBUtils.getUrl(data));
                            urlList.add(new ImageUrl(url, null));
                        }
                        GPreviewBuilder.from(getActivity())
                                .setData(urlList)
                                .setCurrentIndex(i)
                                .setSingleFling(false)//是否在黑屏区域点击返回
                                .setDrag(false)//是否禁用图片拖拽返回
                                .setType(GPreviewBuilder.IndicatorType.Number)//指示器类型
                                .start();//启动
                    }
                });
            }
        };
        superAdapter.setData(photosList);
        return superAdapter;
    }

    public void onActivityResultBitmap(List<String> pathList, String resour) {
        for (String s : pathList) {
            DPictureEntity bean = new DPictureEntity();
            bean.setGuid(StringUtils.getUuid());
            bean.setPGuid(String.valueOf(SPUtils.get(context, SgStrKey.uuidxm, "")));//所属项目ID
            bean.setPPGuid(uuid);//所属到货记录ID
            bean.setPAppaddress(s);
            bean.setPStatus("施工");//固定
            bean.setPDatastate(SgStrKey.add);
            bean.setPPResour(resour);
            bean.setPTime(new Date(System.currentTimeMillis()));
            bean.setPName(s.substring(s.lastIndexOf("/") + 1, s.length()));
            bean.setPLabel("材料,xc");
            photosList.add(0, bean);
        }
        handler.sendEmptyMessage(1);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                if (refresh != null && refresh.isRefreshing()) {
                    refresh.setRefreshing(false);
                }
                superAdapter.notifyDataSetChanged();
                break;
        }
        return false;
    }
}
